package come.bjpowernode.javase.volatileTest;

public class VolatileThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        // 创建VolatileThread线程对象
        VolatileThread volatileThread = new VolatileThread() ;
        volatileThread.start();
        // main方法
        while(true) {
            //Thread.sleep(100);
            //System.out.println("=====");
            if(volatileThread.isFlag()) {
                System.out.println("执行了======");
            }
        }
    }
}

class VolatileThread extends Thread {
    // 定义成员变量
    private volatile boolean flag = false ;
    public  boolean isFlag() { return flag;}
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 将flag的值更改为true
        this.flag = true ;
        System.out.println("flag=" + flag);
    }
}
